package com.hzit.controller;

import com.hzit.dto.StudentDTO;
import com.hzit.entity.PageResult;
import com.hzit.entity.R;
import com.hzit.entity.Student;
import com.hzit.service.StudentService;
import com.hzit.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
	@Autowired
	private StudentService studentService;

	//1. 学生列表
	@GetMapping("/list")
	public R list(){
		List<Student> studentList = studentService.findAll();
		return R.ok(studentList);
	}
	//2. 分页查询
	@GetMapping("/findByPage")
	public R findByPage(int page,int pageSize){
		PageResult<StudentDTO> dtoPageResult = studentService.findByPage(page,pageSize);
		return R.ok(dtoPageResult);
	}
	//3. 条件查询带分页
	@PostMapping("/search")
	public R search(int page, int pageSize, @RequestBody StudentVo vo){
		PageResult<StudentDTO> dtoPageResult = studentService.search(page,pageSize,vo);
		return R.ok(dtoPageResult);
	}
	//4. 添加学生
	@PostMapping("/add")
	public R add(@RequestBody Student student){
		studentService.add(student);
		return R.ok(null,"添加学生成功！");
	}
	//5. 修改学生
	@PostMapping("/update")
	public R update(@RequestBody Student student){
		studentService.update(student);
		return R.ok(null,"修改学生成功！");
	}
	//6. 删除学生 (版本一)
	@GetMapping("delete")
	public R delete(String[] sids){
		studentService.delete(sids);
		return R.ok(null,"删除学生成功！");
	}
	//6. 删除学生 (版本二)
	@GetMapping("delete2")
	public R delete2(Integer[] sids){
		studentService.delete2(sids);
		return R.ok(null,"删除学生成功！");
	}
}
